package cz.data.common.mongo;

import cz.data.common.mongo.manage.MongoManageDialect;
import cz.data.common.mongo.manage.MongoManageFactory;
import cz.data.common.support.YamlPropertySourceFactory;
import cz.data.common.support.db.DbType;
import cz.data.common.support.db.manage.DbManageRegistry;
import cz.data.common.support.db.manage.ManageDialectRegistry;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.context.annotation.PropertySource;

import javax.annotation.PreDestroy;

@AutoConfigureOrder(0)
@AutoConfigureAfter(MongoAutoConfiguration.class)
@PropertySource(value = "classpath:mongodb-setting.yml", factory = YamlPropertySourceFactory.class, encoding = "UTF-8")
public class MongodbAutoConfiguration {

    @PreDestroy
    public void destroy() {
        CacheMongoClientBean.destroy();
    }

    static {
        DbManageRegistry.registry(new MongoManageFactory(), DbType.MONGODB);
        ManageDialectRegistry.registry(new MongoManageDialect(), DbType.MONGODB);
    }
}
